package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 1.添加到购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //1、创建购物车实体对象，限制为只查询当前登录用户的购物车数据
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //2、查询购物车表中是否有完全相同的商品
        List<ShoppingCart> cartList = shoppingCartMapper.select(shoppingCart);
        //3、如果存在相同商品，直接将其数量加1即可
        if (cartList != null && cartList.size() > 0) {
            //获取到查询到的商品数据
            ShoppingCart cart = cartList.get(0);
            //将商品数量加1
            cart.setNumber(cart.getNumber() + 1);
            //更新商品数量
            shoppingCartMapper.updateNumberById(cart);
            return;
        }

        //4、如果不存在相同商品，先判断添加的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        Long setmealId = shoppingCartDTO.getSetmealId();
        if (dishId != null) {
            //是菜品，则到菜品表查询名称、价格、图片
            Dish dish = dishMapper.selectById(dishId);
            //添加名称、价格、图片到购物车实体对象
            shoppingCart.setName(dish.getName());
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setImage(dish.getImage());
        } else {
            //是套餐，则到套餐表查询名称、价格、图片
            Setmeal setmeal = setmealMapper.selectById(setmealId);
            //添加名称、价格、图片到购物车实体对象
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setImage(setmeal.getImage());
        }
        //5、向购物车添加新商品，数量设置为1
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 2.查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> show() {
        //查询当前登录用户的购物车
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.select(shoppingCart);
    }

    /**
     * 3.清空购物车
     */
    @Override
    public void clean() {
        //清空当前登录用户的购物车
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 4.删除购物车中一个商品
     * @param shoppingCartDTO
     */
    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        //1、查询欲删除商品在当前用户购物车中的数量
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> carts = shoppingCartMapper.select(shoppingCart);
        shoppingCart = carts.get(0);
        //2、如果该商品当前数量大于1，则只需要将其数量减1
        if (shoppingCart.getNumber() > 1) {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            //更新商品数量
            shoppingCartMapper.updateNumberById(shoppingCart);
            return;
        }
        //3、商品当前数量为1，则直接删除
        shoppingCartMapper.deleteById(shoppingCart);
    }
}
